package com.xinfu.newproject.ui.pay_cost;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.SuperWebActivity;
import com.xinfu.newproject.adapter.paycost_adapter.PayCostDetailAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.paycost.bean.PayCostDetailBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayCostDetailActivity extends BaseActivity {

    @BindView(R.id.activity_pay_cost_detail_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.activity_pay_cost_detail_status)
    TextView statusTv;

    @BindView(R.id.activity_pay_cost_detail_money)
    TextView moneyTv;

    @BindView(R.id.activity_pay_cost_detail_title)
    TextView titleTv;

    private PayCostDetailAdapter payCostDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_cost_detail);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("账单详情");
        initViewBind();
        initLisener();
    }

    private void initLisener() {
        if (payStatus == 2) {
            statusTv.setText("待支付");
            statusTv.setTextColor(getResources().getColor(R.color.colorAccent));
        } else if (payStatus == 1) {
            statusTv.setText("交易成功");
            statusTv.setTextColor(getResources().getColor(R.color.gray_a5));
        }
        moneyTv.setText(amount);
        payCostDetailAdapter.addFooterView(getFooterView());
    }

    private View getFooterView() {
        View view = getLayoutInflater().inflate(R.layout.footer_pay_cost_detail_invoice, (ViewGroup) mRecyclerView.getParent(), false);
        TextView textView = view.findViewById(R.id.footer_pay_cost_detail_invoice_tv);
        if (list.size() < 7) {
            view.setVisibility(View.GONE);
        } else {
            if (list.get(7).equals("已开票")) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.get(7).equals("已开票")) {
                    String response = getIntent().getExtras().getString("response");
                    JSONObject object = new Gson().fromJson(response, new TypeToken<JSONObject>() {
                    }.getType());
                    Bundle bundle = new Bundle();
                    try {
                        bundle.putString("response", object.getString("pdfUrl"));
                        startIntentActivity(SuperWebActivity.class, bundle);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
//                    Log.i("testInvoice",adapter.getData().get(position).toString());
                }
            }
        });
        return view;
    }


    int payStatus;
    String amount;
    List<String> list;

    private void initViewBind() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        payCostDetailAdapter = new PayCostDetailAdapter();
        mRecyclerView.setAdapter(payCostDetailAdapter);
        String response = getIntent().getExtras().getString("response");
        JSONObject object = new Gson().fromJson(response, new TypeToken<JSONObject>() {
        }.getType());
        PayCostDetailBean payCostDetailBean = new Gson().fromJson(object.toString(), PayCostDetailBean.class);
        amount = payCostDetailBean.getPayFee() + "";
        if (payCostDetailBean.getPayStatus() != null) {
            payStatus = payCostDetailBean.getPayStatus();
        } else {
            payStatus = 0;
        }
        titleTv.setText(payCostDetailBean.getCompanyName());

        list = new ArrayList<>();
        list.add("招商银行(6543)");
        list.add("4785743574834");
        list.add(payCostDetailBean.getBillsRemarks());
        list.add(payCostDetailBean.getRoomName());
        list.add(payCostDetailBean.getCreateTime());
        list.add(payCostDetailBean.getPayTime());
        list.add(payCostDetailBean.getSn());
        if (payCostDetailBean.getInvoiceStatus() == null) {
            list.add("未开票");
        } else {
            list.add(payCostDetailBean.getInvoiceStatus());
        }
        payCostDetailAdapter.setNewData(list);

    }
}
